package com.snow.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-09
 **/
@Configuration
public class DruidConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")//返回的DataSource绑定配置文件
    public DataSource drulidDateSource(){
        return new DruidDataSource();
    }
    //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //1.
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //后台登录账号密码
        HashMap<String, String> parameters = new HashMap<>();
        //增加配置
        parameters.put("loginUsername","watchSnow");
        parameters.put("loginPassword", "196980");//二者键名固定
        //允许谁访问
        parameters.put("allow", "localhost");//值为空语序任何人访问

        //2
        bean.setInitParameters(parameters);
        //3
        return bean;
    }
}
